<!DOCTYPE html><!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--><!--[if gt IE 8]><!--><html class="no-js" lang="en"><!--<![endif]--><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title></title>
  

  
  <link href="https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700" rel="stylesheet" type="text/css">

  <link rel="stylesheet" href="/v2/css/theme.css" type="text/css">
  <link href="/v2/css/custom.css" rel="stylesheet">

  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
  <script src="../../js/theme.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
  <script src="../../search/main.js"></script>

  <style>
    body {font-size: 90%;}
    pre, code {font-size: 100%;}
    h3, h4, h5, h6 {color: #2980b9; font-weight: 300}
  </style> 
<meta name="robots" content="noindex"></head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href="/v2/" class="icon icon-home"> </a>
        <div role="search">
  <form id="rtd-search-form" class="wy-form" action="" method="get">
    <input type="text" name="q" placeholder="Search docs">
    <input type="hidden" name="check_keywords" value="yes">
    <input type="hidden" name="area" value="default">
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
      <ul class="current">
   
  <span class="toctree-l0">Home</span>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/">Welcome</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/imprint/">Imprint</a>
    
  </li>
     
  <span class="toctree-l0">Articles</span>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/articles/getting-started/">Getting started</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/articles/heroku/">Heroku</a>
    
  </li>
  
  <li class="toctree-l1 current">
    <a class="current" href="/v2/articles/express/">Usage with Express.JS</a>
    
    <ul>
      
    </ul>
    
  </li>
     
  <span class="toctree-l0">Docs</span>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/installation/">Installation</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/usage/">Usage</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/promises/">Promises</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/models/">Models</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/instances/">Instances</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/associations/">Associations</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/hooks/">Hooks</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/transactions/">Transactions</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/migrations/">Migrations</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/docs/misc/">Misc</a>
    
  </li>
     
  <span class="toctree-l0">API</span>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/sequelize/">Sequelize</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/model/">Model</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/instance/">Instance</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/associations/">Associations</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/hooks/">Hooks</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/promise/">Promise</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/transaction/">Transaction</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/datatypes/">Datatypes</a>
    
  </li>
  
  <li class="toctree-l1 ">
    <a class="" href="/v2/api/errors/">Errors</a>
    
  </li>
    
</ul>
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="icon icon-reorder"></i>
        <a href="/v2/"></a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="/v2/">Docs</a> »</li>
    <li>Usage with Express.JS</li>
      <li class="wy-breadcrumbs-aside">
        
      </li>
  </ul>
  <hr>
</div>
          <div role="main">
            <div class="section">
              <script type="text/javascript">
    fromGithub = function (partialId, ext) {
        ext = ext || '';

        var $partial = $("#" + partialId)
        $.get("https://cdn.rawgit.com/sequelize/express-example/master/" + partialId.replace("_", "/") + ext, function (code) {

            if (ext === '.js') {
                code = hljs.highlight('js', code).value;
            } 

            code = '<div class="highlight"><pre>' + code + '</pre></div>';

            $partial.replaceWith(code);
        }, 'html');
    }
</script>
<h2 id="introduction">Introduction</h2>
<p>This article explains the usage of Sequelize with the MVC framework Express.You will learn how and where to define models and how to load them when needed.</p>
<h2 id="a-minimal-express-application">A minimal express application</h2>
<p>In order to create a minimal express application, we need to install express first and scaffold a project. We can do this via the following commands:</p>
<pre><code class="language-bash">$ mkdir example-app
$ cd example-app
$ npm install express express-generator
$ node_modules/.bin/express . -f
$ npm install
$ ./bin/www
</code></pre>
<p>You should now be able to see a tiny welcome page on <code>http://localhost:3000</code></p>
<h2 id="adding-sequelize-to-the-application">Adding Sequelize to the application</h2>
<p>Now that we have the express application in place, we can start adding Sequelize to it. What we need for that are the following packages: sequelize, sequelize-cli, sqlite3. Please note, that for the sake of simplicity this tutorial will use SQLite.</p>
<pre><code class="language-bash">$ npm install --save sequelize@2.0.0-rc1 sequelize-cli sqlite3
</code></pre>
<p>This will install the respective packages and uses the upcoming major release of sequelize. We can now let the sequelize CLI initialize the project's directory:</p>
<pre><code class="language-bash">$ node_modules/.bin/sequelize init
</code></pre>
<p>Running this command will create the folders <code>config</code>, <code>migrations</code> and <code>models</code>.</p>
<h2 id="implementing-a-todo-app">Implementing a todo app</h2>
<p>As an example application we will create a very basic and simple todo tool, which allows the creation of users and the management of their tasks.</p>
<h3 id="binwww">bin/www</h3>
<p>In order to create a maintainable application, we will put all the database logic into the <code>models</code> folder. When the application gets fired up, sequelize will sync the models with the database and afterwards start the server. This way we don't clutter the application while making use of sequelize's features.</p>
<div id="bin_www"></div>
<script>$(function () { fromGithub("bin_www") })</script>

<h3 id="modelsindexjs">models/index.js</h3>
<p>This file has been generated with the sequelize CLI and collects all the models from the <code>models</code> directory and associates them if needed.</p>
<div id="models_index"></div>
<script>$(function () { fromGithub("models_index", '.js') })</script>

<h3 id="modelsuserjs">models/user.js</h3>
<p>All models of our application are located as separate files in the <code>models</code> folder. If you want to add a new model, just add it to this folder and everything will work automagically. Also you can use the sequelize CLI's <code>sequelize model:create</code>.</p>
<p><strong>Notice</strong> that the <code>associate</code> method receives a parameter <code>models</code>, which contains every declared model within the models directory.</p>
<div id="models_user"></div>
<script>$(function () { fromGithub("models_user", '.js') })</script>

<h3 id="modelstaskjs">models/task.js</h3>
<p>The other needed model is <code>Task</code>. It relates to the <code>User</code>.</p>
<div id="models_task"></div>
<script>$(function () { fromGithub("models_task", '.js') })</script>

<h3 id="routesindexjs">routes/index.js</h3>
<p>The file <code>routes/index.js</code> contains the logic for a request against the main homepage. It loads the models module and uses it to load all the users and tasks from the database.</p>
<div id="routes_index"></div>
<script>$(function () { fromGithub("routes_index", '.js') })</script>
<p>This will allow us to iterate over the users in the view file. We will skip the rest of the route files for this article.</p>
<h3 id="viewsindexjade">views/index.jade</h3>
<p>As we passed the users to the view and include the tasks for each user, we can access the data in the view's template file. Besides listing the users and tasks, there are also forms for creating new instances.</p>
<div id="views_index"></div>
<script>$(function () { fromGithub("views_index", '.jade') })</script>

<h2 id="whats-next">What's next?</h2>
<p>This article shows a basic approach of how to integrate Sequelize into an ExpressJS application. It allows the very easy management of models by adding new files to a specific folder. Starting the application will automatically sync the schema with the database.</p>
<p>If you don't want to have automatic schema synchronization and instead want migrations, just add a respective step to your deployment script. When you use the CLI for the model generation, you will gain the migration scripts for free as well.</p>
<p>You can find the complete application code <a href="https://github.com/sequelize/express-example">on Github</a>. Feel free to add pull requests to it.</p>
<p>Besides the use of Sequelize as model backend in your ExpressJS application, you might also want to turn your server into a restful API. If that is the case, check out <a href="https://github.com/sequelize/sequelize-restful">the repository on Github</a></p>
            </div>
          </div> 
          <footer>
  

  <hr>

  <div role="contentinfo">
    <p>
    <!-- Copyright etc -->
    </p>
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a>, using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
	  
        </div>
      </div>

    </section>

  </div>

<div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
        
      <span><a href="/v2/" style="color: #fcfcfc;">« Previous</a></span>
      <span style="margin-left: 15px"><a href="/v2/" style="color: #fcfcfc">Next »</a></span>
    </span>
</div>

</body></html>